package com.example.administrator.waitan.find.life;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.tools.VolleySingleton;

import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/1/14.
 */
public class LifeRecyclerViewAdapter extends RecyclerView.Adapter<LifeRecyclerViewAdapter.MyViewHolder>{
    private RequestQueue queue;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String,Bitmap> lruCache;
    private List<LifeInfo> data;
    private Context context;
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public LifeRecyclerViewAdapter(Context context, List<LifeInfo> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rc_life,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        //获取textview对象的高度
        ViewGroup.LayoutParams layoutParams = holder.pic.getLayoutParams();
        Random random = new Random();
        int randomHeight = random.nextInt(400) + 300;
        //将高度设置成随机数
        layoutParams.height = randomHeight;
        holder.pic.setLayoutParams(layoutParams);
        VolleySingleton.getImageLoader().get(data.get(position).getMiddle(),VolleySingleton.getImageListener(holder.pic));
        if (myClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    myClickListener.onClick(pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView pic;
        public MyViewHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.tv_life_pic);
        }
    }
}
